using System;
using System.Collections.Generic;
using System.Text;
using Attach.Internal;
using System.Reflection;

namespace Attach
{
	public class AttachFramework
	{
        private Dictionary<object, Behaviors> behaviors = new Dictionary<object, Behaviors>();

        private Results Attach(object obj, MethodInfo method, Stub stub)
        {
            if (!behaviors.ContainsKey(obj))
            {
                behaviors[obj] = new Behaviors();
            }
            behaviors[obj].Add(method, stub);

            MethodInfo setupBehaviors = obj.GetType().GetMethod("SetupBehaviors", BindingFlags.NonPublic | BindingFlags.Instance);
            setupBehaviors.Invoke(obj, new object[] { behaviors[obj] });

            return stub.Results;
        }

        public Results Attach(Delegate method, Return action)
        {
            return Attach(method.Target, method.Method, new Stub(action));
        }

        public T CreateObject<T>(params object[] constructorParameters)
        {
            return Interceptor.GenerateObject<T>(constructorParameters);
        }
	}
}
